package com.whu.MenuHub.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.whu.MenuHub.domain.Food;
import com.whu.MenuHub.domain.FoodComment;
import com.whu.MenuHub.domain.FoodCommentDTO;
import com.whu.MenuHub.domain.LabelNum;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface FoodCommentDao extends BaseMapper<FoodComment> {

    @Select("SELECT * FROM foodcomment WHERE orderID = #{orderID} ")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "food", column = "foodID",
                    one = @One(select = "com.whu.MenuHub.dao.FoodDao.selectById")),
    })
    List<FoodCommentDTO> findfoodjudgeByOrderID(Integer orderID);

    @Select("select AVG(k.foodscore) from food LEFT JOIN \n" +
            "(SELECT foodcomment.* FROM foodcomment,`user`\n" +
            "where `user`.id=foodcomment.userID AND user.gender=#{gender})as k\n" +
            "on food.ID=K.foodID GROUP BY food.id")
    List<Float> getFoodAveScoreByGender(String gender);

    @Select("select AVG(k.foodscore) from food LEFT JOIN \n" +
            "(SELECT foodcomment.* FROM foodcomment,`user`\n" +
            "where `user`.id=foodcomment.userID AND user.nativeplace=#{address})as k\n" +
            "on food.ID=K.foodID GROUP BY food.id")
    List<Float> getFoodAveScoreByAddress(String address);

    @Select("SELECT * FROM foodcomment WHERE foodID = #{foodID} ")
    List<FoodComment> findfoodjudgeByFoodID(Integer foodID);
    @Select("SELECT count(*) FROM foodcomment WHERE foodID = #{foodID}")
    int findOrderNumberByFoodID(Integer foodID);

    @Select("SELECT * FROM foodcomment WHERE orderId = #{orderID} limit 1")
    FoodComment findOneFoodCommentByOrderID(Integer orderID);

    @Select("SELECT * FROM foodcomment ${ew.customSqlSegment}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "user", column = "userID",
                    one = @One(select = "com.whu.MenuHub.dao.UserDao.selectById")),
    })
    public IPage<FoodCommentDTO> findFoodComments(IPage <FoodCommentDTO> page,
                                                  @Param(Constants.WRAPPER) QueryWrapper<FoodCommentDTO> wrapper);

    @Select("SELECT food.label,sum(foodcomment.foodNumber) as count from foodcomment,food " +
            "where foodcomment.foodID=food.id and foodcomment.userID=#{userID} " +
            "GROUP BY food.label")
    public List<LabelNum> getlabelNumByUserOrder(Integer userID);
}
